import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [{
    path: '/',
    redirect: '/home',
  }, {
    path: '/home',
    component: () => import('../views/HomePageView.vue')
  },
  {
    path: '/workPlaceView',
    component: () => import('../views/WorkPlaceView.vue'),
    children: [{
        path: 'about',
        name: 'AboutView',
        component: () => import('../views/AboutView.vue')
      }, {
        path: 'test',
        component: () => import('../views/TestView.vue')
      }, {
        path: 'storeMange',
        component: () => import('../components/StoreMangeView.vue')
      },
      {
        path: 'clothesMange',
        component: () => import('../components/ClothesMangeView.vue')
      },
      {
        path: 'saleMange',
        component: () => import('../components/SaleMangeView.vue')
      },
      
      {
        path: 'purchaseMange',
        component: () => import('../components/PurchaseMangeView.vue')
      },
      {
        path: 'customerMange',
        component: () => import('../components/CustomerMangeView.vue')
      },
      {
        path: 'activityMange',
        component: () => import('../components/ActivityMangeView.vue')
      },
      {
        path: 'employeeMange',
        component: () => import('../components/EmployeeMangeView.vue')
      },
      {
        path: 'costMange',
        component: () => import('../components/CostMangeView.vue')
      },
      {
        path: 'dataMange',
        component: () => import('../components/DataMangeView.vue')
      },
      {
        path: 'userMessage',
        component: () => import('../components/UserMessageView.vue')
      },
    ]
  },
  {
    path: '/adminWorkplace',
    component: () => import('../views/AdminPlaceView.vue'),
    children: [{
        path: 'adminUser',
        component: () => import('../components/AdminUserView.vue')
      },
      {
        path: 'adminStore',
        component: () => import('../components/AdminStoreView.vue')
      },
      {
        path: 'adminMessage',
        component: () => import('../components/AdminMessageView.vue')
      },
      {
        path: 'adminMange',
        component: () => import('../components/AdminAdminMangeView.vue')
      },
    ]
  },
]
const router = new VueRouter({
  routes, // 简化语法，等价于routes: routes  
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  // 目标路由不是登录页，并且还需要token验证，还没有token，那就直接给返回到登录页
  if (to.path === from.path) {
    return next(false);
  }
  if (token || to.path === "/home") {
    return next();
  } else {
    return next('/home'); // 否则，允许访问  
  }
})
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push

// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace

// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
export default router;